import { createStore } from 'vuex'
import {testData,testPosts,ColumnProps,PostProps} from '../testData'
interface UserProps {
  isLogin:boolean;
  name?:string;
  id?:number;
}
export interface GolbalDataProps{
  columns:ColumnProps[];
  posts:PostProps[],
  user:UserProps,
}

const store =createStore<GolbalDataProps>({
  state: {
    columns:testData,
    posts:testPosts,
    user:{
      isLogin:false
    },
  },
  mutations: {
    login(state){
      state.user = {...state.user,isLogin:true,name:'Jimmy'}
    },
    addPosts(state,obj){
      state.posts.push(obj)
    }
  },
  actions: {
  },
  modules: {
  },
  getters:{
    // getters 相当于是 computed计算属性  只要是state中数据错误了就会重新计算
    getSelectColumnType:(state)=>(id:number)=>{
      return state.columns.find(item=>item.id===Number(id))
    },
    getPostByCid:(state)=>(cid:number)=>{
      return state.posts.filter(item=>item.columnid===Number(cid))
    }
  }
})

export default store
